*&---------------------------------------------------------------------*
*& Report ZPPR128
*&---------------------------------------------------------------------*
*&  技改工单
*&  事务码： ZPP138
*&---------------------------------------------------------------------*
REPORT zppr128.

INCLUDE zppr128_top.
INCLUDE zppr128_m01.

INITIALIZATION.
  bt1   = '下载模板'.
  bt2   = '选择文件'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL METHOD zcl_excel=>get_filename.

AT SELECTION-SCREEN .
  CASE sy-ucomm.
    WHEN 'DOWNLOAD' .
      CALL METHOD zcl_excel=>download .
    WHEN 'UPLOAD'.
      CALL METHOD zcl_excel=>get_filename.
  ENDCASE.

START-OF-SELECTION.

  DATA: lo_excel TYPE REF TO zcl_excel,
        lo_data  TYPE REF TO zcl_data,
        lo_alv   TYPE REF TO zcl_alv.

  lo_excel  = NEW zcl_excel( ) .
  lo_data   = NEW zcl_data( ) .
  lo_alv    = NEW zcl_alv( lo_data ) .

  lo_excel->read_excel( ) .
  lo_data->query_and_check( ) .
  lo_alv->display( ) .


CLASS zcl_data IMPLEMENTATION.

  METHOD constructor.

  ENDMETHOD .

  METHOD run .



    LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs_data>) WHERE sel = 'X' AND status = ''.

      CLEAR: mv_prod_order .

      MOVE-CORRESPONDING <fs_data> TO ms_data .

      create_planorder( ) .
      bdc_plan_prod( ) .
      add_component( ) .
      add_operation( ) .

      IF mv_error = 'X'.
        <fs_data>-status = gc_red_light .
      ELSE.
        <fs_data>-order  = mv_prod_order.
        <fs_data>-status = gc_green_light .
      ENDIF.
      <fs_data>-message = mv_message .
      CLEAR: mv_message .

    ENDLOOP.

  ENDMETHOD.

**  METHOD check_alv_selection .
**
**    DATA: ls_data LIKE LINE OF gt_data .
**
**    CLEAR: mv_error .
**
**    READ TABLE gt_data INTO ls_data WITH KEY sel = 'X' .
**    IF sy-subrc <> '0'.
**      MESSAGE s000(oo) WITH '没有选中任何行' DISPLAY LIKE 'E' .
**      mv_error = 'X' .
**    ENDIF.
**
**  ENDMETHOD.

  METHOD query_and_check .

    DATA: BEGIN OF l_material,
            matnr TYPE matnr,
            werks TYPE werks_d,
            charg TYPE charg_d,
            sernr TYPE objk-sernr,
          END OF l_material .
    DATA: lt_material LIKE TABLE OF l_material,
          ls_material LIKE LINE OF lt_material.

    LOOP AT gt_data INTO gs_data.
      ls_material-matnr = gs_data-matnr_orgin.
      ls_material-werks = gs_data-werks.
      ls_material-charg = gs_data-charg_orgin.
      ls_material-sernr = gs_data-sernr_orgin.
      APPEND ls_material TO lt_material .
    ENDLOOP.

    SELECT mspr~matnr,mspr~werks,mspr~charg
      FROM mspr
        INNER JOIN @lt_material AS m ON mspr~matnr = m~matnr AND mspr~werks = m~werks AND mspr~charg = m~charg
    INTO TABLE @DATA(lt_mspr) .

    SELECT r~matnr, r~werks , r~charg , r~sernr
      FROM zvmm_sernrkc AS r
        INNER JOIN @lt_material AS m ON r~matnr = m~matnr AND r~werks = m~werks
                                    AND r~charg = r~charg AND r~sernr = m~sernr
    INTO  TABLE @DATA(lt_sernr) .

    SELECT m~mblnr ,
           m~aufnr ,
           m~cpudt,
           m~matnr ,
           m~werks ,
           m~charg ,
           objk~sernr
       FROM matdoc AS m
      INNER JOIN ser03 ON m~mblnr = ser03~mblnr AND m~mjahr = ser03~mjahr AND m~zeile = ser03~zeile
      INNER JOIN objk ON ser03~obknr = objk~obknr
      INNER JOIN @lt_material AS a ON m~matnr = a~matnr AND m~werks = a~werks AND m~charg = a~charg
                               AND objk~sernr = a~sernr
      WHERE m~bwart = '101'
      INTO CORRESPONDING FIELDS OF TABLE @mt_mblnr .
    SORT mt_mblnr BY cpudt DESCENDING .

    DATA:BEGIN OF l_aufnr_stat,
           aufnr TYPE aufnr,
           stat  TYPE pordarch-stat,
         END OF l_aufnr_stat .
    DATA: lt_aufnr_stat LIKE TABLE OF l_aufnr_stat,
          ls_aufnr_stat LIKE LINE OF lt_aufnr_stat.
    DATA: BEGIN OF l_aufnr,
            aufnr TYPE aufk-aufnr,
          END OF l_aufnr .
    DATA: lt_aufnr LIKE TABLE OF l_aufnr .

    MOVE-CORRESPONDING mt_mblnr TO lt_aufnr .
    SORT lt_aufnr .DELETE ADJACENT DUPLICATES FROM lt_aufnr COMPARING ALL FIELDS .

    SELECT p~aufnr , p~stat
      FROM pordarch AS p
          INNER JOIN @lt_aufnr AS a ON p~aufnr = a~aufnr
      INTO TABLE @lt_aufnr_stat .

    SELECT afko~aufnr ,
           afvc~vornr ,
           afvc~ltxa1 ,
           afvc~steus
        FROM afko
        INNER JOIN afvc ON afko~bedid = afvc~bedid
        INNER JOIN @lt_aufnr AS a ON afko~aufnr = a~aufnr
    INTO CORRESPONDING FIELDS OF TABLE @mt_operation  .

    DATA: lt_zppt018 TYPE TABLE OF zppt018 .
    SELECT *
        INTO TABLE lt_zppt018
      FROM zppt018 .

    LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs_data>).

      IF NOT line_exists( lt_mspr[ matnr = <fs_data>-matnr_orgin
                                   werks = <fs_data>-werks
                                   charg = <fs_data>-charg_orgin  ] ).
        <fs_data>-message = |此物料{ <fs_data>-matnr_orgin ALPHA = OUT }无对应批次库存请修改|  .
      ENDIF.

      IF NOT line_exists( lt_sernr[ matnr = <fs_data>-matnr_orgin
                                    werks = <fs_data>-werks
                                    charg = <fs_data>-charg_orgin
                                    sernr = <fs_data>-sernr_orgin ] ).
        <fs_data>-message = |批次序列错误请修改|  .
      ENDIF.

      READ TABLE mt_mblnr INTO DATA(ls_mblnr) WITH KEY matnr = <fs_data>-matnr_orgin
                                                       werks = <fs_data>-werks
                                                       charg = <fs_data>-charg_orgin.
      IF sy-subrc =  '0'.
        <fs_data>-repair_reason = |{ ls_mblnr-aufnr }{ <fs_data>-repair_reason }| .
        <fs_data>-aufnr_orgin   = ls_mblnr-aufnr .
        READ TABLE lt_aufnr_stat INTO ls_aufnr_stat WITH KEY aufnr = ls_mblnr-aufnr stat = 'E0002' .
        IF sy-subrc <> '0'.
          <fs_data>-message = |原工单没有进入MES管理请SAP自行处理| .
        ENDIF.
      ENDIF.

      IF NOT line_exists( lt_zppt018[ zzmodelcode = <fs_data>-zzmodelcode
                                   zzmodelnumber = <fs_data>-zzmodelnumber
                                   zzprodline = <fs_data>-zzprodline ] ).
        <fs_data>-message = '请用ZPP085维护产线数据后创建订单' .
      ENDIF.

      IF  <fs_data>-message IS NOT INITIAL.
        CONDENSE <fs_data>-message.
        <fs_data>-status = gc_red_light .
      ENDIF.

    ENDLOOP.

  ENDMETHOD.


  METHOD add_operation .

    DATA: lt_operation TYPE cose_t_operation,
          ls_operation LIKE LINE OF lt_operation.
    DATA: ls_return TYPE bapiret2.

    IF mv_prod_order IS INITIAL.
      RETURN .
    ENDIF.

    wait_and_removelock( ) .

    " 这里绝对可以找到
    READ TABLE mt_mblnr INTO ms_mblnr WITH KEY matnr = ms_data-matnr_orgin
                                               werks = ms_data-werks
                                               charg = ms_data-charg_orgin
                                               sernr = ms_data-sernr_orgin .

    SORT mt_operation BY aufnr vornr .

    LOOP AT mt_operation INTO DATA(ls_opt) WHERE aufnr = ms_mblnr-aufnr.

      ls_operation-operation     = ls_opt-vornr. " 工序
      ls_operation-control_key   = ls_opt-steus. " 控制码
      ls_operation-work_center   = 'JX001'.      " 工作中心
      ls_operation-work_center_x = 'X'.
      ls_operation-short_text    = ls_opt-ltxa1. " 短文本
      ls_operation-short_text_x  = 'X'.

      APPEND ls_operation TO lt_operation.
    ENDLOOP.

    CALL FUNCTION 'CO_SE_PRODORD_CHANGE'
      EXPORTING
        iv_order_number = mv_prod_order
*       IS_HEADER       =
*       IT_HDR_USER_STATUS       =
*       IT_SEQUENCE     =
        it_operation    = lt_operation
*       IT_OPR_USER_STATUS       =
*       IT_PRT          =
*       IV_COMMIT       =
      IMPORTING
        es_return       = ls_return.

    IF ls_return-type = 'E'.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      mv_error = 'X' .
      mv_message = |{ mv_message }{ ls_return-message }| .
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
    ENDIF.

  ENDMETHOD.

  METHOD add_component .

    " 参考 ZPPR071 1485

    IF mv_prod_order IS INITIAL.
      RETURN .
    ENDIF.

    wait_and_removelock( ) .

    DATA: lt_component    TYPE TABLE OF bapi_alm_order_component,
          ls_component    TYPE bapi_alm_order_component,
          ls_component_up TYPE bapi_alm_order_component_up,
          lt_component_up TYPE TABLE OF bapi_alm_order_component_up,
          lt_methods      TYPE TABLE OF bapi_alm_order_method,
          ls_methods      TYPE bapi_alm_order_method.

    DATA: lt_return TYPE STANDARD TABLE OF bapiret2.
    DATA: lv_com_index TYPE ifrefnum.

    ls_methods-method          = 'CREATE'.
    ls_methods-refnumber       = 1.
    ls_methods-objecttype      = 'COMPONENT' .
    ls_methods-objectkey+0(12) = mv_prod_order.
    ls_methods-objectkey+12(4) = '0010'.
    APPEND ls_methods TO lt_methods .

    ls_component-activity               = '0010'." 工序编号
    ls_component-item_cat               = 'L'." 项目类别
    ls_component-material               = ms_data-matnr_orgin.
    ls_component-batch                     = ms_data-charg_orgin.
    ls_component-requirement_quantity   = 1." 需求数量
    ls_component-requirement_quantity_unit = 'EA' . " 单位
    APPEND ls_component TO lt_component.

    ls_component_up-activity                  = 'X'.
    ls_component_up-item_cat                  = 'X'.
    ls_component_up-material                  = 'X'.
    ls_component_up-batch                     = 'X'.
    ls_component_up-requirement_quantity      = 'X'.
    ls_component_up-requirement_quantity_unit = 'X' .

    APPEND ls_component_up TO lt_component_up.

    CLEAR ls_methods.
    ls_methods-method = 'SAVE'.
    APPEND ls_methods TO lt_methods.

    DATA: l_auart TYPE aufk-auart.
    FIELD-SYMBOLS: <t350> TYPE t350.
    SELECT SINGLE auart INTO l_auart
       FROM aufk WHERE aufnr = mv_prod_order.
    CALL FUNCTION 'T350_READ'
      EXPORTING
        auart         = l_auart
      EXCEPTIONS
        no_t350_entry = 1
        OTHERS        = 2.

* override buffered table T350
    ASSIGN ('(SAPLINST)T350') TO <t350>.
    IF sy-subrc = 0.
      <t350>-auart = l_auart.
    ENDIF.

    DO 30 TIMES.
      CALL FUNCTION 'BAPI_ALM_ORDER_MAINTAIN'
        TABLES
          it_methods      = lt_methods
          it_component    = lt_component
          it_component_up = lt_component_up
          return          = lt_return.

      READ TABLE lt_return TRANSPORTING NO FIELDS WITH KEY type = 'E' id = 'IWO_BAPI' number = '124'.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      WAIT UP TO '1' SECONDS.
    ENDDO.


    LOOP AT lt_return INTO DATA(ls_return) WHERE type = 'E' OR type = 'A'.
    ENDLOOP.
    IF sy-subrc =  '0'.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      mv_error = 'X' .
      mv_message = |添加工序失败：{ ls_return-message }| .
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
    ENDIF.


  ENDMETHOD.

  METHOD create_planorder .

    DATA: ls_header  TYPE  bapiplaf_i1,
          ls_return  TYPE bapireturn1,
          plan_order TYPE bapi_pldord-pldord_num.

    IF mv_error = 'X'.
      RETURN .
    ENDIF.

    ls_header-pldord_profile = 'PR' .
    ls_header-material = ms_data-matnr .
    ls_header-material = |{ ls_header-material ALPHA = IN WIDTH = 18 }| .
    ls_header-plan_plant = ms_data-werks.
    ls_header-prod_plant = ms_data-werks..
    ls_header-total_plord_qty = '1'.

    ls_header-order_start_date = ms_data-begda..
    ls_header-order_fin_date   = ms_data-endda.
    ls_header-acctasscat = 'Q'.
    ls_header-base_uom = 'EA'.
    ls_header-mrp_area = ms_data-werks.


    SELECT COUNT(*) FROM marc
      WHERE matnr = ls_header-material
       AND  verkz = 'X' .
    IF sy-subrc <>  '0'.
      MESSAGE s000(oo) WITH |请维护物料{ ms_data-matnr }生产版本|  DISPLAY LIKE 'E' .
      mv_error = 'X' .
      EXIT.
    ENDIF.

    SELECT SINGLE pspnr
      INTO ls_header-int_project_num
      FROM prps
      WHERE posid = ms_data-posid .

    CALL FUNCTION 'BAPI_PLANNEDORDER_CREATE'
      EXPORTING
        headerdata   = ls_header
      IMPORTING
        return       = ls_return
        plannedorder = mv_plan_order.

    IF ls_return-type = 'E' OR ls_return-type = 'A'.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      mv_error = 'X' .
      mv_message = |计划订单创建失败{ ls_return-message }| .
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
      mv_message = |计划订单{ mv_plan_order }创建| .

    ENDIF.

  ENDMETHOD.

  METHOD  bapi_plan_prod.

    DATA: order_number TYPE afpo-aufnr,
          ls_bapiret2  TYPE bapiret2.

    sy-batch = 'X' .

    CALL FUNCTION 'BAPI_PRODORD_CREATE_FROM_PLORD'
      EXPORTING
        planned_order    = mv_plan_order
        order_type       = 'ZP04'
*       ORDER_NUMBER     =
      IMPORTING
        production_order = order_number
*       PROD_ORDER_TYPE  =
        return           = ls_bapiret2.


  ENDMETHOD.

  METHOD bdc_plan_prod .

    DATA: lt_bdcmsg  TYPE TABLE OF bdcmsgcoll,
          ls_bdcmsg  LIKE LINE OF lt_bdcmsg,
          lt_bdcdata TYPE TABLE OF bdcdata,
          ls_bdcdata LIKE LINE OF lt_bdcdata.

    DATA opt TYPE ctu_params.

    IF mv_error = 'X'.
      RETURN .
    ENDIF.

    SELECT  SINGLE verid
      INTO ms_data-verid
       FROM plaf
      WHERE plnum = mv_plan_order .

    opt-dismode = 'S'.
    opt-defsize = 'X'.


    lt_bdcdata = VALUE #(

" 初始界面
      ( program = 'SAPLCOKO1' dynpro = '0150' dynbegin = 'X' )
      ( fnam = 'BDC_CURSOR'        fval = 'AFPOD-PLNUM' )
      ( fnam = 'AFPOD-PLNUM'       fval = mv_plan_order )
      ( fnam = 'BDC_OKCODE'        fval = '=ENTK' )
      ( fnam = 'AUFPAR-PP_AUFART'  fval = 'ZP04' )

" 修改数量
      ( program = 'SAPLCOKO1' dynpro = '0115' dynbegin = 'X' )
      ( fnam = 'BDC_SUBSCR'      fval = 'SAPLCOKO1                               0120SUBSCR_0115' )
*      ( fnam = 'CAUFVD-GLTRP'    fval = lv_endda )
*      ( fnam = 'CAUFVD-GSTRP'    fval = lv_begda )
      ( fnam = 'CAUFVD-TERKZ'    fval = '2' )
      ( fnam = 'CAUFVD-GAMNG'    fval = '1' )
      ( fnam = 'CAUFVD-GMEIN'    fval = 'EA' )
      ( fnam = 'BDC_OKCODE'      fval = '=SLAP' )

" 主数据界面修改版本
      ( program = 'SAPLCOKO1' dynpro = '0115' dynbegin = 'X' )
      ( fnam = 'BDC_SUBSCR'      fval = 'SAPLCOKO1                               0250SUBSCR_0115' )
      ( fnam = 'BDC_CURSOR'      fval = 'CAUFVD-PLNNR                               0250SUBSCR_0115' )
      ( fnam = 'BDC_OKCODE'      fval = '=STAK' )
      ( program = 'SAPLCOKO1' dynpro = '0131' dynbegin = 'X' )
      ( fnam = 'BDC_SUBSCR'      fval = 'RC62F-NO_SEL' )
      ( fnam = 'RC62F-VER_SEL'    fval = '' )
      ( fnam = 'RC62F-PROD_VERS'    fval = ms_data-verid )
      ( fnam = 'RC62F-NO_SEL'    fval = 'X' )
      ( fnam = 'RC62F-NEW_ROUT'    fval = 'X' )
      ( fnam = 'RC62F-NEW_ROUT'    fval = 'X' )
      ( fnam = 'BDC_OKCODE'      fval = '=ENT1' )
      ( program = 'SAPLCOSD'   dynpro = '0310' dynbegin = 'X' )
      ( fnam = 'BDC_OKCODE'      fval = '=OPT1' )

" 自定义界面数据
      ( program = 'SAPLCOKO1'   dynpro = '0115' dynbegin = 'X' )
      ( fnam = 'BDC_SUBSCR'      fval = 'SAPLCOKO1                               0250SUBSCR_0115' )
      ( fnam = 'BDC_OKCODE'      fval = '=+COI' )

      ( program = 'SAPLCOKO1'   dynpro = '0115' dynbegin = 'X' )
      ( fnam = 'BDC_SUBSCR'      fval = 'SAPLCOKO1                               0900SUBSCR_0115' )
      ( fnam = 'BDC_SUBSCR'      fval = 'SAPLXCO1                                0100SSCR115' )
      ( fnam = 'GS_COCI_AUFK-ZZMODELCODE'      fval = ms_data-zzmodelcode )
      ( fnam = 'GS_COCI_AUFK-ZZMODELNUMBER'    fval = ms_data-zzmodelnumber )
      ( fnam = 'GS_COCI_AUFK-ZZPRODLINE'       fval = ms_data-zzprodline )
      ( fnam = 'GS_COCI_AUFK-ZZGRUND'          fval = ms_data-repair_reason )
      ( fnam = 'BDC_OKCODE'      fval = '/00' )

" 填写标题栏序列号
      ( program = 'SAPLCOKO1'   dynpro = '0115' dynbegin = 'X' )
      ( fnam = 'BDC_OKCODE'      fval = '=SERA' )
      ( program = 'SAPLIPW1'    dynpro = '0300' dynbegin = 'X' )
      ( fnam = 'RIPW0-SERNR(01)'   fval = ms_data-sernr )
      ( fnam = 'BDC_OKCODE'        fval = '=RWS' )

" 保存
      ( program = 'SAPLCOKO1'   dynpro = '0115' dynbegin = 'X' )
      ( fnam = 'BDC_SUBSCR'      fval = 'SAPLCOKO1                               0250SUBSCR_0115' )
      ( fnam = 'BDC_OKCODE'      fval = '=BU' )

    ) .

    CALL TRANSACTION 'CO40'  USING lt_bdcdata
                        OPTIONS FROM opt
                        MESSAGES INTO lt_bdcmsg.

    LOOP AT lt_bdcmsg INTO ls_bdcmsg  WHERE msgtyp = 'E' OR msgtyp = 'A'.
      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
        EXPORTING
          msgid               = ls_bdcmsg-msgid
          msgnr               = ls_bdcmsg-msgnr
          msgv1               = ls_bdcmsg-msgv1
          msgv2               = ls_bdcmsg-msgv2
          msgv3               = ls_bdcmsg-msgv3
        IMPORTING
          message_text_output = mv_message.
    ENDLOOP.
    IF sy-subrc <> '0'.
      COMMIT WORK AND WAIT .
      READ TABLE lt_bdcmsg INTO ls_bdcmsg WITH KEY tcode = 'CO40' dyname = 'SAPLCOKO1' msgtyp = 'S'
                                                  dynumb = '0115' msgid = 'CO' msgnr = '100' .
      IF sy-subrc =  '0'.
        mv_prod_order = |{ ls_bdcmsg-msgv1 ALPHA = IN WIDTH = 12 }|.
        mv_message = |生产订单{ mv_prod_order }创建|.
      ENDIF.
    ELSE.
      mv_error = 'X' .
    ENDIF.

  ENDMETHOD.

  METHOD call_bapi2 .

    RETURN .  " 弃用

    IF mv_prod_order IS INITIAL.
      RETURN .
    ENDIF.

    wait_and_removelock( ) .

    DATA: lt_methods      TYPE TABLE OF bapi_alm_order_method,
          ls_methods      TYPE bapi_alm_order_method,
          lt_operation    TYPE TABLE OF bapi_alm_order_operation,
          ls_operation    TYPE bapi_alm_order_operation,
          lt_operation_up TYPE TABLE OF bapi_alm_order_operation_up,
          ls_operation_up TYPE bapi_alm_order_operation_up.

    ls_methods-refnumber = 1.
    ls_methods-method = 'CHANGE' .
    APPEND ls_methods TO lt_methods .

    CLEAR: ls_operation .
    ls_operation-activity    = '0010' .
    ls_operation-plant       = 'P020'.
    ls_operation-work_cntr   = 'JX001'. " 工作中心
    ls_operation-control_key = 'PP01' .
    ls_operation-description = |工序10文本|.
    ls_operation-quantity    = 1.
    ls_operation-base_uom    = 'EA'.
    APPEND ls_operation TO lt_operation .

    CLEAR: ls_operation_up .
    ls_operation_up-activity    = 'X' .
    ls_operation_up-plant       = 'X' .
    ls_operation_up-work_cntr   = 'X' .
    ls_operation_up-control_key = 'X' .
    ls_operation_up-description = 'X' .
    ls_operation_up-quantity    = 'X' .
    ls_operation_up-base_uom    = 'X' .
    APPEND ls_operation_up TO lt_operation_up .


    ls_methods-objecttype = 'OPERATION' .
    ls_methods-objectkey+0(12) = mv_prod_order.
    ls_methods-objectkey+12(4) = '0010'.
    APPEND ls_methods TO lt_methods .

    CLEAR ls_methods.
    ls_methods-method = 'SAVE'.
    APPEND ls_methods TO lt_methods.


    DATA: lt_return TYPE STANDARD TABLE OF bapiret2.
    DATA: l_auart   TYPE aufk-auart.

    FIELD-SYMBOLS: <t350> TYPE t350.
    SELECT SINGLE auart INTO l_auart
      FROM aufk WHERE aufnr = mv_prod_order.
    CALL FUNCTION 'T350_READ'
      EXPORTING
        auart         = l_auart
      EXCEPTIONS
        no_t350_entry = 1
        OTHERS        = 2.

* override buffered table T350
    ASSIGN ('(SAPLINST)T350') TO <t350>.
    IF sy-subrc = 0.
      <t350>-auart = l_auart.
    ENDIF.

    CALL FUNCTION 'BAPI_ALM_ORDER_MAINTAIN'
      TABLES
        it_methods      = lt_methods
        it_operation    = lt_operation
        it_operation_up = lt_operation_up
*       EXTENSION_IN    =
        return          = lt_return.

    LOOP AT lt_return INTO DATA(ls_return) WHERE type = 'E' OR type = 'A'.

    ENDLOOP.
    IF sy-subrc = 0.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
    ENDIF.

  ENDMETHOD.

  METHOD wait_and_removelock .

    DO 200 TIMES.

      SELECT COUNT(*)
         FROM aufk
        WHERE aufnr = mv_prod_order .
      IF sy-subrc <> '0'.
        WAIT UP TO '0.3' SECONDS .
      ELSE.
        EXIT.
      ENDIF.

    ENDDO.

    DATA: lt_enq TYPE STANDARD TABLE OF seqg7.
    DATA: lv_garg TYPE seqg3-garg.
    lv_garg = sy-mandt && mv_prod_order.

    DO  50 TIMES.
      CLEAR: lt_enq.

      CALL FUNCTION 'ENQUE_READ2'
        EXPORTING
          gclient = sy-mandt
          garg    = lv_garg
        TABLES
          enq     = lt_enq.
      IF lt_enq IS INITIAL.
        EXIT.
      ENDIF.

      WAIT UP TO '0.1' SECONDS.
    ENDDO.


  ENDMETHOD.



ENDCLASS .